home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / jwpsrc.zip / KINFOIDX.C < prev    next >
C/C++ Source or Header  |  1993-03-31  |  3KB  |  125 lines

  1. /* Copyright (C) Stephen Chung, 1991-1993.  All rights reserved. */
  2.  
  3. #include <stdio.h>
  4.  
  5. typedef struct {
  6.     unsigned char strokes;
  7.     unsigned char bushu;
  8.     unsigned short int index;
  9.     unsigned char grade;
  10.     unsigned char on:4;
  11.     unsigned char kun:4;
  12.     unsigned char unknown:4;
  13.     unsigned char meanings:4;
  14.     unsigned char padding;           /* To pad it to 32-bits */
  15.     unsigned long int offset;
  16. } KINFORECORD;
  17.  
  18. typedef struct {
  19.     unsigned char strokes;
  20.     unsigned char bushu;
  21.     short int index;
  22. } IDXRECORD;
  23.  
  24. IDXRECORD rec[7000];
  25.  
  26.  
  27. int BushuStrokes(IDXRECORD *s1, IDXRECORD *s2)
  28. {
  29.     if (s1->bushu == s2->bushu) {
  30.         if (s1->strokes == s2->strokes) {
  31.             return (s1->index - s2->index);
  32.         } else {
  33.             return (s1->strokes - s2->strokes);
  34.         }
  35.     } else {
  36.         return (s1->bushu - s2->bushu);
  37.     }
  38. }
  39.  
  40.  
  41.  
  42. int StrokesBushu(IDXRECORD *s1, IDXRECORD *s2)
  43. {
  44.     if (s1->strokes == s2->strokes) {
  45.         if (s1->bushu == s2->bushu) {
  46.             return (s1->index - s2->index);
  47.         } else {
  48.             return (s1->bushu - s2->bushu);
  49.         }
  50.     } else {
  51.         return (s1->strokes - s2->strokes);
  52.     }
  53. }
  54.  
  55.  
  56. short int IndexToJIS(int index)
  57. {
  58.     int div, rem;
  59.     short int ch;
  60.  
  61.     div = index / 94;
  62.     rem = index % 94;
  63.  
  64.     ch = (div << 8) | rem;
  65.     ch += 0x3021;
  66.  
  67.     return (ch);
  68. }
  69.  
  70.  
  71.  
  72. main()
  73. {
  74.     long int i, j;
  75.     FILE *ifp, *ofp;
  76.     KINFORECORD record;
  77.     int num;
  78.  
  79.     ifp = fopen("kinfo.dat", "rb");
  80.  
  81.     fseek(ifp, 0L, 0);
  82.     fread(&record, sizeof(KINFORECORD), 1, ifp);
  83.     num = record.offset / sizeof(KINFORECORD);
  84.  
  85.     printf("%d records...\n", num);
  86.  
  87.     fseek(ifp, 0L, 0);
  88.  
  89.     for (i = j = 0; i < num; i++) {
  90.         if (i % 10 == 9) printf("Record #%d...\r", i);
  91.         fread(&record, sizeof(KINFORECORD), 1, ifp);
  92.         if (record.strokes <= 0 || record.bushu <= 0) continue;
  93.  
  94.         rec[j].index = IndexToJIS(i);
  95.         rec[j].bushu = record.bushu;
  96.         rec[j].strokes = record.strokes;
  97.         j++;
  98.     }
  99.  
  100.     num = j;
  101.  
  102.     printf("%d records indexed\n", j);
  103.  
  104.     fclose(ifp);
  105.  
  106.     printf("Creating index...\n");
  107.  
  108.     ofp = fopen("kinfo.idx", "wb");
  109.  
  110.     printf("Sorting Bushu / Strokes...\n");
  111.  
  112.     qsort(rec, num, sizeof(IDXRECORD), StrokesBushu);
  113.  
  114.     fseek(ofp, 0L, 0);
  115.     fwrite(rec, sizeof(IDXRECORD), num, ofp);
  116.  
  117.     printf("Sorting Strokes / Bushu...\n");
  118.  
  119.     qsort(rec, num, sizeof(IDXRECORD), BushuStrokes);
  120.  
  121.     fwrite(rec, sizeof(IDXRECORD), num, ofp);
  122.  
  123.     fclose(ofp);
  124. }
  125.